import ctime from '@/components/changeTime.vue'
import defaultImg from '@/components/defaultImg/defaultImg'
import { share } from '@/utils/common-api.js'
export default {
  data() {
    return {
      dataList: [
        {
          name: '全部',
          checked: true,
          value: 0,
        },
        {
          name: '待报价',
          checked: false,
          value: 1,
        },
        {
          name: '已报价',
          checked: false,
          value: 2,
        },
        {
          name: '已过期',
          checked: false,
          value: 3,
        },
        {
          name: '我的订单',
          checked: false,
          value: 4,
        },
      ],
      offerCountList: ['待报价', '已报价', '已过期', '已取消'],
      curNow: 0, //当前状态
      showimg: false, //默认为空显示图片
      storeShow: false, //店铺列表弹窗
      pageindex: 1,
      myFindCarlist: [],
      show: false,
      xuncarid: 0,
      specification: [],
      totalpage: 0,
      loadText: ['loadmore', 'loading', 'nomore'],
      loadTextIndex: 0,
      findthecarStatus: false, //上架状态
      activeId: '', //当前选择的id 上架下架使用
      onShelfTime: '', //选择上架时间
      isShelf: null, //上架下架
    }
  },
  onLoad() {
    this.getSpecification()
    this.getOrderList()
  },
  components: {
    ctime,
    defaultImg,
  },
  onReachBottom() {
    let zs = Math.ceil(this.totalpage / 10)
    if (zs > this.pageindex) {
      this.loadTextIndex = 1
      this.pageindex++
      this.getOrderList()
    } else {
      this.loadTextIndex = 2
    }
  },
  onBackPress(options) {
    if (options.from === 'navigateBack') {
      return false
    }
    this.back()
    return true
  },
  methods: {
    back() {
      uni.navigateTo({
        url: '/pages_findCar/findtheCar/findtheCar',
      })
    },
    // 获取规格
    async getSpecification() {
      try {
        const res = await this.$api.jsonData.specification()
        this.specification = res.data
      } catch (err) {
        console.log(err)
      }
    },
    fliterSpecification(id) {
      let name = ''
      if (id !== '') {
        // console.log(this.specification)
        this.specification.forEach(it => {
          if (it.id == id) {
            name = it.name
          }
        })
        return name
      }
    },
    // 获取列表数据
    async getOrderList() {
      let data = {
        pageIndex: this.pageindex,
        pageSize: 10,
        type: this.curNow,
        store_Id: this.$store.state.userInfoDetail.store.storeId,
      }
      try {
        const res = await this.$api.findCar.GetCarFinBMylist(data)
        this.myFindCarlist = this.myFindCarlist.concat(res.data.list)
        this.totalpage = res.data.total
        let zs = Math.ceil(this.totalpage / 10)
        if (zs < this.pageindex) {
          this.loadTextIndex = 2
        }

        // if (this.totalpage === 0) {
        //   this.showimg = false
        // } else {
        this.showimg = true
        // }
      } catch (err) {
        this.showimg = true
        console.log(err)
      }
    },
    // 上架下架弹框
    Inquiryonshelves(item, type) {
      this.activeId = item.id
      this.isShelf = type
      if (type === 1) {
        this.findthecarStatus = true
      } else {
        this.show = true
      }
    },
    // 取消寻车
    setFindTheCarStatus() {
      let data = {
        id: this.activeId,
        type: this.isShelf,
        deadline: null,
      }
      this.$api.findCar
        .UpdateState(data)
        .then(res => {
          console.log(res)
          this.show = false
          uni.showToast({
            title: '已下架！',
            duration: 2000,
            icon: 'success',
          })
          this.myFindCarlist = []
          this.pageindex = 1
          this.getOrderList()
        })
        .catch(err => {
          //    uni.showToast({
          // title: '已下架！',
          // duration: 2000,
          // icon: 'success'
          //    })
        })
    },
    // #ifdef APP-PLUS
    share(id) {
      // 分享方法
      share(
        {
          type: 0,
          summary: '最新求购车源，速戳',
          href:
            'https://ztcbs.yzsheng.com/#/pages_findCar/noQuotationForFindCar/noQuotationForFindCar?storeId=' +
            this.$store.state.userInfoDetail.store.storeId +
            '&type=1&id=' +
            id,
        },
        status => {
          if (status) {
            uni.hideToast() // 关闭生成中提示
          } else {
            uni.showToast({
              title: '分享失败',
              duration: 2000,
            })
          }
        }
      )
    },
    // #endif
    // 上架
    confirmFindTheCarStatus(e) {
      var myDate = new Date()
      // let year=myDate.getFullYear()
      // let month=myDate.getMonth()+1
      // let day=myDate.getDate()
      // let today=year + '-' + month + '-' + day
      this.onShelfTime = ''
      this.findthecarStatus = false
      this.onShelfTime = e.year + '-' + e.month + '-' + e.day

      // 当前时间e
      if (new Date(this.onShelfTime) > myDate) {
        // 0下架，1上架
        let data = {
          id: this.activeId,
          type: this.isShelf,
          deadline: this.onShelfTime,
        }
        this.$api.findCar
          .UpdateState(data)
          .then(res => {
            console.log(res)
            uni.showToast({
              title: '上架成功！',
              duration: 2000,
              icon: 'success',
            })
            this.myFindCarlist = []
            this.pageindex = 1
            this.getOrderList()
          })
          .catch(err => {
            //    uni.showToast({
            // title: '请重试！',
            // duration: 2000,
            // icon: 'none'
            //    })
          })
      } else {
        uni.showToast({
          title: '上架时间不能低于当前时间',
          duration: 2000,
          icon: 'none',
        })
        this.onShelfTime = ''
        return
      }
    },
    backTime(it) {
      if (it) {
        let month = it.split('T')[0].split('-')
        let timeList = it.split('T')[1].split(':')
        let time =
          month[1] + '-' + month[2] + ' ' + timeList[0] + ':' + timeList[1]
        return time
      }
    },
    gethideshow(phNum) {
      if (phNum) {
        return phNum.substr(0, 1) + '**'
      }
    },
    //发布寻车
    chakanwxc() {
      uni.navigateTo({
        url: '/pages_findCar/releaseForCar/releaseForCar?type=1',
      })
    },
    //详情
    searlclick(item) {
      if (item.state === 1) {
        uni.navigateTo({
          url:
            '/pages_findCar/hasQuotedForTheCar/hasQuotedForTheCar?goodsid=' +
            item.id,
        })
      } else {
        uni.navigateTo({
          url:
            '/pages_findCar/detailsForTheCar/detailsForTheCar?goodsid=' +
            item.id,
        })
      }
    },
    //编辑
    Inquiryedit(id) {
      uni.navigateTo({
        url: '/pages_findCar/releaseForCar/releaseForCar?type=2&id=' + id,
      })
    },
    //点击样式停留
    ChooseSelector(index) {
      console.log(index)
      for (var i = 0; i < this.dataList.length; i++) {
        this.dataList[i].checked = false
      }
      if (index === 4) {
        uni.navigateTo({
          url: '/pages_order/orderAdmin/orderAdmin',
        })
      } else {
        this.dataList[index].checked = true
        this.curNow = index
        this.pageindex = 1
        this.myFindCarlist = []
        this.getOrderList()
      }
    },
    //取消寻车
    quxiaoclick(typeid) {
      this.show = true
      this.xuncarid = typeid
    },
    //我想想
    woxiangx() {
      this.show = false
    },
  },
}
